package threadedserver;

import java.awt.*;
import java.net.*;
import java.io.*;

public class ConnectionThread extends Thread {

    private Socket sock;
    private TextArea messages;
    private int connectionNumber;

    public ConnectionThread(Socket sock, TextArea messages, int connectionNumber) {
        this.sock = sock;
        this.messages = messages;
        this.connectionNumber = connectionNumber;
    }

//Thread which deals with a connection from a client
    public void run() {
        try {
            messages.append("...Connection " + connectionNumber + " established\n");
            BufferedReader bf = new BufferedReader(new InputStreamReader(sock.getInputStream()));
            //Reads a string for a client
            bf.readLine();
            //Following line simulates some long process such as a database access
            Thread.sleep(1000);
            //Close down
            bf.close();
            sock.close();
            messages.append("...Connection " + connectionNumber + " relinquished\n");
        } catch (Exception e) {
            System.out.println("Problem getting a stream " + e.getMessage());
        }
    }
}